namespace IAM.Interface.Common.XMLResolution
{
    using IAM.Interface.Model;
    using IAM.Interface.Model.ENUM;
    using System;
    using System.Text;
    using System.Xml;

    public class Request : AbstractXMLResolution
    {
        private IAM.Interface.Model.ENUM.ActionType _actiontype;
        private IAM.Interface.Model.ENUM.OperationType _operationtype;
        private TransmissionParameter _tranpar;
        private string _transactionid;

        public Request()
        {
            this._actiontype = IAM.Interface.Model.ENUM.ActionType.Nullity;
            this._operationtype = IAM.Interface.Model.ENUM.OperationType.Nullity;
            this._tranpar = null;
            this._transactionid = "";
        }

        public Request(IAM.Interface.Model.ENUM.ActionType actiontype, IAM.Interface.Model.ENUM.OperationType operationtype, TransmissionParameter tranpar, string xmlinfo)
        {
            this._actiontype = IAM.Interface.Model.ENUM.ActionType.Nullity;
            this._operationtype = IAM.Interface.Model.ENUM.OperationType.Nullity;
            this._tranpar = null;
            this._transactionid = "";
            this._actiontype = actiontype;
            this._operationtype = operationtype;
            this._tranpar = tranpar;
            base._xmlinfo = xmlinfo;
        }

        public IAM.Interface.Model.ENUM.ActionType GetActionType()
        {
            IAM.Interface.Model.ENUM.ActionType at = IAM.Interface.Model.ENUM.ActionType.Nullity;
            try
            {
                switch (base.XmlDocument.DocumentElement.Attributes["ActionType"].InnerText)
                {
                    case "IDEN_MANAGE":
                        at = IAM.Interface.Model.ENUM.ActionType.IDEN_MANAGE;
                        goto Label_0142;

                    case "IDEN_GETACMODEL":
                        at = IAM.Interface.Model.ENUM.ActionType.IDEN_GETACMODEL;
                        goto Label_0142;

                    case "IDEN_GETUSERACI":
                        at = IAM.Interface.Model.ENUM.ActionType.IDEN_GETUSERACI;
                        goto Label_0142;

                    case "IDEN_SETUSERACI":
                        at = IAM.Interface.Model.ENUM.ActionType.IDEN_SETUSERACI;
                        goto Label_0142;

                    case "ORG_UPDATE":
                        at = IAM.Interface.Model.ENUM.ActionType.ORG_UPDATE;
                        goto Label_0142;

                    case "ORG_CREATE":
                        at = IAM.Interface.Model.ENUM.ActionType.ORG_CREATE;
                        goto Label_0142;

                    case "ORG_DELETE":
                        at = IAM.Interface.Model.ENUM.ActionType.ORG_DELETE;
                        goto Label_0142;

                    case "DM_GETMODULELIST":
                        at = IAM.Interface.Model.ENUM.ActionType.DM_GETMODULELIST;
                        goto Label_0142;

                    case "DM_GETDELEINFO":
                        at = IAM.Interface.Model.ENUM.ActionType.DM_GETDELEINFO;
                        goto Label_0142;

                    case "DM_SETDELEINFO":
                        at = IAM.Interface.Model.ENUM.ActionType.DM_SETDELEINFO;
                        goto Label_0142;
                }
                at = IAM.Interface.Model.ENUM.ActionType.Nullity;
            }
            catch
            {
                at = IAM.Interface.Model.ENUM.ActionType.Nullity;
            }
        Label_0142:
            this._actiontype = at;
            return at;
        }

        public override XmlNode GetCurrentNode()
        {
            XmlElement xe = base.XmlDocument.DocumentElement;
            switch (xe.GetAttribute("ActionType"))
            {
                case "IDEN_MANAGE":
                    return xe.SelectSingleNode("Identity");

                case "IDEN_GETACMODEL":
                    return xe;

                case "IDEN_GETUSERACI":
                    return xe.SelectSingleNode("AccountInfo");

                case "IDEN_SETUSERACI":
                    return xe;

                case "ORG_UPDATE":
                    return xe;

                case "ORG_CREATE":
                    return xe;

                case "ORG_DELETE":
                    return xe;

                case "DM_GETMODULELIST":
                    return xe;

                case "DM_GETDELEINFO":
                    return xe.SelectSingleNode("AccountInfo");

                case "DM_SETDELEINFO":
                    return xe;

                case "IDEN_USERLIST":
                    return xe;

                case "AUDIT_FETCH":
                    return xe.SelectSingleNode("Logs");
            }
            return null;
        }

        public IAM.Interface.Model.ENUM.OperationType GetOperationType()
        {
            XmlElement ex = base.XmlDocument.DocumentElement;
            string strActionType = ex.Attributes["ActionType"].InnerText;
            IAM.Interface.Model.ENUM.OperationType ot = IAM.Interface.Model.ENUM.OperationType.Create;
            if (!(strActionType == "IDEN_MANAGE"))
            {
                ot = IAM.Interface.Model.ENUM.OperationType.Nullity;
            }
            else
            {
                string opType = ex.SelectSingleNode("OperationType").InnerText.Trim();
                if (opType != null)
                {
                    if (!(opType == "Create"))
                    {
                        if (opType == "Enable")
                        {
                            ot = IAM.Interface.Model.ENUM.OperationType.Enable;
                        }
                        else if (opType == "Disable")
                        {
                            ot = IAM.Interface.Model.ENUM.OperationType.Disable;
                        }
                        else if (opType == "Delete")
                        {
                            ot = IAM.Interface.Model.ENUM.OperationType.Delete;
                        }
                        else if (opType == "Update")
                        {
                            ot = IAM.Interface.Model.ENUM.OperationType.Update;
                        }
                    }
                    else
                    {
                        ot = IAM.Interface.Model.ENUM.OperationType.Create;
                    }
                }
            }
            this._operationtype = ot;
            return ot;
        }

        public string GetTransactionID()
        {
            try
            {
                XmlElement ex = base.XmlDocument.DocumentElement;
                this._transactionid = ex.Attributes["TransactionID"].InnerText;
                return ex.Attributes["TransactionID"].InnerText;
            }
            catch
            {
                return "";
            }
        }

        public override string GetWholeXML()
        {
            RequestData requestData = new RequestData();
            IdenManageOperationType imot = null;
            requestData.TransactionID = Guid.NewGuid().ToString();
            requestData.RequestTime = DateTime.Now;
            requestData.AppName = this.TranPar.AppName;
            requestData.Token = Guid.NewGuid().ToString();
            requestData.ActionType = this.ActionType;
            if (this.ActionType == IAM.Interface.Model.ENUM.ActionType.IDEN_MANAGE)
            {
                imot = new IdenManageOperationType();
                switch (this.OperationType)
                {
                    case IAM.Interface.Model.ENUM.OperationType.Create:
                        imot.OperationType = IAM.Interface.Model.ENUM.OperationType.Create;
                        break;

                    case IAM.Interface.Model.ENUM.OperationType.Enable:
                        imot.OperationType = IAM.Interface.Model.ENUM.OperationType.Enable;
                        break;

                    case IAM.Interface.Model.ENUM.OperationType.Disable:
                        imot.OperationType = IAM.Interface.Model.ENUM.OperationType.Disable;
                        break;

                    case IAM.Interface.Model.ENUM.OperationType.Delete:
                        imot.OperationType = IAM.Interface.Model.ENUM.OperationType.Delete;
                        break;

                    case IAM.Interface.Model.ENUM.OperationType.Update:
                        imot.OperationType = IAM.Interface.Model.ENUM.OperationType.Update;
                        break;
                }
            }
            StringBuilder xml = new StringBuilder();
            xml.Append(requestData.XMLBoday);
            if (imot != null)
            {
                xml.Append(imot.XML);
            }
            xml.Append(base.XmlInfo);
            xml.Append(requestData.XMLFinis);
            return xml.ToString();
        }

        public IAM.Interface.Model.ENUM.ActionType ActionType
        {
            get
            {
                if (this._actiontype == IAM.Interface.Model.ENUM.ActionType.Nullity)
                {
                    return this.GetActionType();
                }
                return this._actiontype;
            }
            set
            {
                this._actiontype = value;
            }
        }

        public IAM.Interface.Model.ENUM.OperationType OperationType
        {
            get
            {
                if (this._operationtype == IAM.Interface.Model.ENUM.OperationType.Nullity)
                {
                    return this.GetOperationType();
                }
                return this._operationtype;
            }
            set
            {
                this._operationtype = value;
            }
        }

        public TransmissionParameter TranPar
        {
            get
            {
                return this._tranpar;
            }
            set
            {
                this._tranpar = value;
            }
        }

        public string TransactionID
        {
            get
            {
                if (this._transactionid == "")
                {
                    return this.GetTransactionID();
                }
                return this._transactionid;
            }
            set
            {
                this._transactionid = value;
            }
        }
    }
}
